//给定一个二叉树，找出其最大深度。 
//
// 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 
//
// 说明: 叶子节点是指没有子节点的节点。 
//
// 示例： 
//给定二叉树 [3,9,20,null,null,15,7]， 
//
//     3
//   / \
//  9  20
//    /  \
//   15   7 
//
// 返回它的最大深度 3 。 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 1065 👎 0

package leetcode.editor.cn;

import java.util.*;

public class _104_MaximumDepthOfBinaryTree {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public static void main(String[] args) {
        int n = Integer.MAX_VALUE;
        int[] nums = new int[]{3, 9, 20, n, n, 15, 7, n, n, n, n, n, n, n, n};
        TreeNode root = creatTree(nums, 0);
        Solution solution = new _104_MaximumDepthOfBinaryTree().new Solution();
        System.out.println(solution.maxDepth(root));
    }

    /**
     * 层序创建二叉树
     */
    private static TreeNode creatTree(int[] nums, int index) {
        if (nums[index] == Integer.MAX_VALUE) {
            return null;
        }
        TreeNode root = new TreeNode(nums[index]);
        root.left = creatTree(nums, 2 * index + 1);
        root.right = creatTree(nums, 2 * index + 2);
        return root;
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        /**
         * 迭代，广度优先搜索
         * @param root
         * @return
         */
        public int maxDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int res = 0;
            Queue<TreeNode> q = new LinkedList<>();
            q.add(root);
            while (!q.isEmpty()) {
                int size = q.size();
                while (size-- > 0) {
                    TreeNode tmp = q.poll();
                    if (tmp.left != null) q.add(tmp.left);
                    if (tmp.right != null) q.add(tmp.right);
                }
                res++;
            }
            return res;
        }

        /**
         * 官方解法，递归
         * @param root
         * @return
         */
        public int maxDepth_digui(TreeNode root) {
            if (root == null) {
                return 0;
            }
            return Math.max(maxDepth_digui(root.left), maxDepth_digui(root.right)) + 1;


            // 利用栈实现中序遍历
//            System.out.println(method_2(root));

            // 先来个先序遍历
//            method_1(root);
        }

        private List<Integer> method_2(TreeNode root) {
            List<Integer> nums = new ArrayList<>();
            if (root == null) {
                return nums;
            }
            Stack<TreeNode> s = new Stack<>();
            while (!s.isEmpty() || root != null) {
                while (root != null) {
                    s.push(root);
                    root = root.left;
                }
                root = s.pop();
                nums.add(root.val);
                root = root.right;
            }
            return nums;
        }

        private void method_1(TreeNode root) {
            List<Integer> nums = new ArrayList<>();
            DFSPre(root, nums);
            System.out.println(nums);
        }

        public void DFSPre(TreeNode root, List<Integer> nums) {
            if (root == null) {
                return;
            }

            nums.add(root.val);
            DFSPre(root.left, nums);
            DFSPre(root.right, nums);


        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}